<template>
  <div class="box">
    <div class="lucky-img">
      <img src="@/assets/img/luck-spin.png" alt="" srcset="" />
    </div>

    <my-notice />
    <section class="truns">
      <div class="bag-box" @click="openBag">
        <img src="@/assets/img/gift.png" alt="" />
        <span class="bag-name">my rewards</span>
        <span v-if="detail.hasAwards" class="bag-mark"> </span>
      </div>

      <img class="round-icon" src="@/assets/img/round.png" alt="" />
      <div :class="trunClass">
        <img src="@/assets/img/trun.png" alt="" class="trun-icon" />
      </div>
      <img
        src="@/assets/img/btn.png"
        alt=""
        class="start-btn"
        @click="getStart"
      />
      <img src="@/assets/img/hand.png" alt="" :class="handClass" />
      <div class="trun-count">
        <!-- <span class="text">{{ `Number of Lucky Rolling ${lotteryNum}` }}</span> -->
        <span class="text">{{
          detail.lotteryNum > 0
            ? `Draw: Free ${detail.lotteryNum} times`
            : 'Rolling: 10 coins'
        }}</span>
      </div>
    </section>
    <!-- <gift-show /> -->
    <bag-modal ref="bagModal" />
    <get-gift-modal ref="getGiftModal" />-·
    <gift-detail-modal ref="giftDetailModal" />
    <MissGiftModal ref="missGiftModal" />
    <NotEnoughModal ref="notEnoughModal" />
    <BackPageModal ref="backPageModal" />
    <RecommendGiftModal ref="recommendGiftModal" />
  </div>
</template>

<script>
import BagModal from './components/BagModal.vue'
import GetGiftModal from './components/GetGiftModal.vue'
import MyNotice from '@/components/MyNotice.vue'
import GiftShow from './components/GiftShow.vue'
import MissGiftModal from './components/MissGiftModal.vue'
import GiftDetailModal from './components/GiftDetailModal.vue'
import NotEnoughModal from './components/NotEnoughModal.vue'
import BackPageModal from './components/BackPageModal.vue'
import { parseUrlParams, clearWebHistory } from '@/utils/util.js'
import device from 'current-device'
import RecommendGiftModal from './components/RecommendGiftModal.vue'

import { mapState } from 'vuex'
export default {
  name: 'TrunIndex',
  components: {
    BagModal,
    GetGiftModal,
    MyNotice,
    GiftShow,
    MissGiftModal,
    GiftDetailModal,
    NotEnoughModal,
    BackPageModal,
    RecommendGiftModal
  },
  data() {
    return {
      hasTrun: false,
      detail: '',
      bagList: []
    }
  },
  computed: {
    ...mapState({
      hasNativeBack: 'hasNativeBack'
    }),
    lotteryNum() {
      const { lotteryNum = 0 } = this.detail
      return lotteryNum
    },
    trunClass() {
      let str = 'trun-img'
      if (this.hasTrun) {
        str += ' trun-ani'
      }
      return str
    },
    handClass() {
      let str = 'hand-point'
      if (this.hasTrun) {
        str += ' on-trun-hidden'
      }
      return str
    }
  },
  watch: {
    hasNativeBack(newVal) {
      if (newVal === 1) {
        this.$store.commit('changeNativeBack')
        this.getBagList()
      }
    }
  },
  // beforeCreate() {
  //   clearWebHistory()
  // },
  created() {
    this.getDetail(1)
    clearWebHistory()
  },
  methods: {
    async getDetail(type = 0) {
      const { code, data = [] } = await this.$request({
        url: '/api/v2/activity/turntable/person/info',
        loading: type !== 2
      })
      if (code === 200) {
        this.detail = data
        if (type === 1) {
          this.initPage()
        }
      } else {
        this.detail = {
          lotteryNum: 0
        }
      }
    },
    async getBagList() {
      const { code, data = [] } = await this.$request({
        url: '/api/v2/activity/turntable/backpack'
      })
      // 先判断礼物
      if (code === 200 && data.length > 0) {
        const buytList = data
          .filter((val) => {
            val.price = val.price * 1
            return val.price
          })
          .sort((a, b) => a.price - b.price)
        // bloomer android单独处理
        if (device.os === 'android' && buytList.length > 0) {
          console.log('buytList==', buytList)
          this.$refs.recommendGiftModal.showModal(buytList[0])
          return false
        }
        this.$refs.backPageModal
          .showModal({
            type: 2
          })
          .then(() => {
            this.openBag()
          })
        return false
      }
      // 2、判断抽奖次数
      const { lotteryNum } = this.detail
      if (lotteryNum > 0) {
        this.$refs.backPageModal.showModal({
          type: 1
        })
        return false
      }
      this.$jsBridge.closePage()
    },
    initPage() {
      const urlParams = parseUrlParams()
      if (urlParams.pageType == 2) {
        this.openBag()
      }
    },
    async getStart() {
      if (this.hasTrun) {
        return false
      }
      const { lotteryNum = 0, coins = 0 } = this.detail
      if (lotteryNum < 1) {
        // 金币抽奖
        const coinTrunNum = Math.floor(coins / 10)
        if (coinTrunNum < 1) {
          this.$refs.notEnoughModal.showModal()
          return false
        }
        this.$jsBridge.updateCoin()
        this.detail.coins -= 10
      } else {
        // 免费抽奖
      }
      this.hasTrun = true
      const dom = document.querySelector('.trun-img')
      dom.style.transition = ''
      dom.style.transform = 'rotate(0)'
      this.beforeRound()
      const { code, data = [] } = await this.$request({
        url: '/api/v2/activity/turntable/lottery'
      })
      if (code === 200) {
        const { backpack } = data

        this.startRound(backpack)
      }
    },
    beforeRound() {
      const roundCount = 20
      const dom = document.querySelector('.trun-img')
      const random = Math.floor(Math.random() * roundCount) * 60
      const degNum = roundCount * 360 + random
      dom.style.transition = '10s cubic-bezier(0, 0.71, 0.18, 1)'
      dom.style.transform = `rotate(${degNum}deg)`
    },
    startRound(backpack) {
      const { location, type } = backpack
      const dom = document.querySelector('.trun-img')
      const random = Math.abs(location - 7) * 60
      const degNum = 10 * 360 + random
      const trunSecord = Math.floor(Math.random() * 4 + 3)
      dom.style.transition = `${trunSecord}s cubic-bezier(0, 0.71, 0.18, 1)`
      dom.style.transform = `rotate(${degNum}deg)`

      const { lotteryNum } = this.detail
      this.detail.lotteryNum = lotteryNum - 1
      const delay = trunSecord * 800
      console.log('dela==', delay)
      setTimeout(() => {
        switch (type) {
          case 1:
            this.$refs.missGiftModal.showModal().then(() => {
              this.tryArgin()
            })
            break
          case 2:
          case 3:
          case 4:
            this.$refs.getGiftModal.showModal(backpack).then(() => {})
            break
          default:
            break
        }
        this.hasTrun = false
        if (this.detail.lotteryNum < 1) {
          this.getDetail(2)
        }
      }, delay)
    },
    tryArgin() {
      setTimeout(() => {
        this.getStart()
      }, 1000)
    },

    openBag() {
      this.$refs.bagModal.showModal().then(() => {
        this.getDetail()
      })
    }
  }
}
</script>
<style lang="less" scoped>
.box {
  background: #4e0684;
  width: 100%;
  min-height: 100vh;
  padding-top: 0.8rem;
  .lucky-img {
    width: 90%;
    margin: 0 auto 0.2rem;
  }
  .truns {
    width: 90%;
    margin: 0 auto;
    position: relative;
    .bag-box {
      position: absolute;
      top: -0.8rem;
      left: -0.1rem;
      width: 1.35rem;
      z-index: 1;
      .bag-name {
        font-size: 0.23rem;
        color: #000000;
        position: absolute;
        bottom: 0.15rem;
        height: 0.32rem;
        line-height: 0.32rem;
        left: 0;
        width: 100%;
        text-align: center;
      }
      .bag-mark {
        position: absolute;
        right: 0.1rem;
        top: -0.1rem;
        width: 0.3rem;
        height: 0.3rem;
        line-height: 0.3rem;
        text-align: center;
        background: #f00;
        color: #fff;
        border-radius: 50%;
        transform: scale(0.7);
      }
    }
    .round-icon {
      width: 100%;
      position: absolute;
      left: 0;
      top: -0.3rem;
    }
    .trun-img {
      border-radius: 50%;
      width: 6.1rem;
      height: 6.1rem;
      overflow: hidden;
      margin: 0 auto;
      // transition: 3s cubic-bezier(0, 0.71, 0.18, 1);
      .trun-icon {
        width: 100%;
      }
    }
    .start-btn {
      position: absolute;
      left: calc(50% - 0.76rem);
      top: calc(50% - 1.2rem);
      width: 1.5rem;
    }
    .hand-point {
      position: absolute;
      width: 1rem;
      right: 28%;
      top: 3rem;
      pointer-events: none;
      animation: ani-hand-point 0.6s infinite;
      transform-origin: bottom center;
    }
    .trun-count {
      color: #fff;
      font-size: 0.27rem;
      text-align: center;
      position: absolute;
      left: calc(50% - 2.1rem);
      width: 4.2rem;
      height: 1rem;
      line-height: 1rem;
      bottom: -1.8rem;
      background: url('@/assets/img/luck-count.png') no-repeat 0 0/ 100%;
    }
    .on-trun-hidden {
      opacity: 0;
      transition: 1s;
    }
  }
}
</style>
